var app = angular.module('webapp');

app.controller('AssistController', ['$scope','AssistService',AssistController]);

app.directive('editBox',['AssistService',function(AssistService){
	return {
		name : 'E',
		scope:{
			ngModel:'=',
			type:'@'
		},
		replace:true,
		template:'<i class="glyphicon glyphicon-edit operateIcon" ng-click="startEdit()"></i>',
		link:function(scope , element , attrs , controller){
			scope.startEdit = function(){
				$('#editContent').val(scope.ngModel.CONTENT);
				$('#editModal').modal('show');
				$('#sureEdit').unbind('click').click(function(){
					scope.$parent.editAssist(scope.ngModel,scope.type);
				});
			};
		}
	};
}]);
app.directive('removeDir',['AssistService',function(AssistService){
	return {
		name : 'E',
		scope:{
			ngModel:'='
		},
		replace:true,
		template:'<i class="glyphicon glyphicon-remove operateIcon" ng-click="startRemove()"></i>',
		link:function(scope , element , attrs , controller){
			scope.startRemove = function(){
				$('#deleteModal').modal('show');
				$('#sureDel').unbind('click').click(function(){
					scope.$parent.removeAssist(scope.ngModel);
				});
			};
		}
	};
}]);
function AssistController($scope , AssistService){

	$scope.editContent = '';
	$scope.assistName = document.getElementById('assistName').value;

	$scope.addAssist = function(){
		$('#editContent').val('');
		$('#editModal').modal('show');
		$('#sureEdit').unbind('click').click(function(){
			$scope.editAssist({},1);
		});
	};
	//type0编辑  1新增
	$scope.editAssist = function(model , type){
		var postModel = {
			name : $scope.assistName,
			id:model.ID ? model.ID : '',
			content:$scope.editContent
		};

		if(type == 0){
			AssistService.updateAssist(postModel).then(function(result){
				messageMinErr('编辑成功');
				$scope.loadAssist();
			},function(err){
				alert(err);
			});
		}else if(type == 1){
			AssistService.addAssist(postModel).then(function(result){
				messageMinErr('新增成功');
				$scope.loadAssist();
			},function(err){
				alert(err);
			});
		}
		$('#editModal').modal('hide');
	};

	$scope.loadAssist = function(){
		AssistService.queryAssistAll({name:$scope.assistName}).then(function(result){
			$scope.assistData = result;
		},function(err){
			alert(err);
		});
	};
	$scope.removeAssist = function(model){
		var name = document.getElementById('assistName').value;
		AssistService.removeAssist({id:model.ID,name:$scope.assistName}).then(function(result){
			messageMinErr('删除成功');
			$scope.loadAssist();
		},function(err){
			alert(err);
		});
		$('#deleteModal').modal('hide');
	};

	$scope.loadAssist();
}